#!/bin/bash

# 配置环境变量
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export LANG=zh_CN.UTF-8
export PATH=/usr/local/node/bin:$PATH

# 定义颜色
color_blue_start="\033[34m"
color_blue_end="\033[0m"
color_green_start="\033[32m"
color_green_end="\033[0m"

function install_dependencies() {
        npm install pnpm -g --registry=https://registry.npmmirror.com
        npm install cnpm -g --registry=https://registry.npmmirror.com
        npm config set registry https://registry.npmmirror.com
        pnpm config set registry https://registry.npmmirror.com
}
# 安装 alemon-bot
function alemon-bot_install {
    # 修改搜索bot_id
    sed -r -i "s/(Bot_id:).*/\1 1/"  config.yaml
    # 检查配置项是否已经存在
    if grep -q "AlemonPath" config.yaml; then
        echo "配置项已存在..."
    else
        # 将配置项写入文件
        echo "AlemonPath: \"./Alemon_Bot\"" >> config.yaml
        echo "配置项已写入..."
    fi
    install_dependencies
    if [ ! -d "alemon-bot" ]; then
        npm install alemon-cli -g
        { echo; sleep 0.5; echo; } | alemon-cli install
    else
        read -p "alemon-bot 已经安装，是否重新安装 (y/n)？" is_install
        case $is_install in
            y)
                rm -rf alemon-bot
                npm install alemon-cli -g
                { echo; sleep 0.5; echo; } | alemon-cli install
                ;;
            n)
                echo "退出安装"
                return
                ;;
            *)
                echo "退出安装"
                return
                ;;
        esac
    fi
    cd alemon-bot
    npm install
    # 输出安装完成信息
    echo -e "${color_green_start} alemon-bot 安装完成 ${color_green_end}"
    echo -e "${color_blue_start} 请参考视频教程进行配置：xxx ${color_blue_end}"
    read -p "按回车键继续" is_install
}

# 安装 TRSS-Yunzai
function TRSS-Yunzai_install {
    echo -e "${color_blue_start} 此脚本仅适用于频道，如需安装QQ机器人，请参考作者 Gitee 地址：https://gitee.com/TimeRainStarSky/Yunzai ${color_blue_end}"
    sed -r -i "s/(Bot_id:).*/\1 2/"  config.yaml
    # 检查配置项是否已经存在
    if grep -q "YunZaiPath" config.yaml; then
        echo "配置项已存在..."
    else
        # 将配置项写入文件
        echo "YunZaiPath: \"./Yunzai\"" >> config.yaml
        echo "配置项已写入..."
    fi
    install_dependencies
    if [ ! -d "Yunzai" ]; then
        git clone --depth 1 https://gitee.com/TimeRainStarSky/Yunzai
    else
        read -p "TRSS-Yunzai 已经安装，是否重新安装 (y/n)？" is_install
        case $is_install in
            y)
                rm -rf Yunzai
                git clone --depth 1 https://gitee.com/TimeRainStarSky/Yunzai
                ;;
            n)
                echo "退出安装"
                return
                ;;
            *)
                echo "退出安装"
                return
                ;;
        esac
    fi
    cd Yunzai
    pnpm install -P
    # 输出安装完成信息
    echo -e "${color_green_start} TRSS-Yunzai 安装完成 ${color_green_end}"
    Yunzai_plugins_install
}

# 安装 Yunzai 插件
function Yunzai_plugins_install {
    git clone --depth 1 https://gitee.com/TimeRainStarSky/Yunzai-genshin plugins/genshin
    git clone --depth 1 https://gitee.com/yoimiya-kokomi/miao-plugin plugins/miao-plugin
    git clone --depth 1 https://gitee.com/TimeRainStarSky/Yunzai-QQGuild-Plugin plugins/QQGuild-Plugin
    pnpm install -P
    # 插件
    echo -e "${color_green_start} Yunzai 插件安装完成 ${color_green_end}"
    echo -e "${color_blue_start} 请按照视频教程进行配置：https://www.bilibili.com/video/BV1mz4y1E7Zf?t=58.4 ${color_blue_end}"
    #按回车键继续
    read -p "安装完成输入box，完成初始化1,按回车键退出" is_install
    
}

# 主菜单
function main_menu() {
    clear
    echo -e "${color_blue_start}
	=======欢迎使用一键安装V1=========
	  1. 安装/重新安装 alemon-bot
	  2. 安装/重新安装 TRSS-Yunzai
	  0. 退出/主菜单"
    read -p "       请输入菜单编号：" run_function
    case $run_function in
        1)
            curl -sL https://gitee.com/Liplay-1/YBOX/raw/master/software/Environment.sh > /root/Environment.sh && bash /root/Environment.sh
            alemon-bot_install
            ;;
        2)
            curl -sL https://gitee.com/Liplay-1/YBOX/raw/master/software/Environment.sh > /root/Environment.sh && bash /root/Environment.sh
            TRSS-Yunzai_install
            ;;
        0)
            exit 0
            ;;
        *)  
            main_menu
            ;;
    esac
}

# 传入的第一个参数为 Go_BOX_PATH
Go_BOX_PATH=$1
if [ -d $Go_BOX_PATH ]; then
    cd $Go_BOX_PATH
fi
main_menu